Ext.define('TS.controller.Emp', {
    extend: 'Ext.app.Controller',
    models: ['base.EmpModel','base.DeptModel'],
    //requires:['TS.store.base.EmpStore','TS.store.base.DeptStore'],
    stores: [
        'base.EmpStore','base.DeptStore'
    ],
    views: [
        'base.EmpList','base.DeptList','base.EmpInfo','base.EmpEditer','base.DeptEditer'
    ],
    init: function() {
        this.control({ 
        	'emplist':{
        		itemdblclick: this.editEmp 
        	},
            'emplist button[action=del]': {
                click: this.delEmp
        	},
        	'emplist button[action=upd]': {	
                click: this.btnEditEmp
        	},
        	'emplist button[action=add]': {     		
                click: this.btnAddEmp
        	},
        	'empediter button[action=save]': { 
                click: this.updateEmp 
            },
        	/*'deptlist':{
        		beforeitemdblclick: this.editDept 
        	},*/
        	'deptlist':{
        		select:this.loadEmpByDeptId
        	},
        	'deptlist button[action=del]':{
        		click: this.delDept        	
        	},
        	'deptlist button[action=add]':{
        		click: this.btnAddDept        	
        	},
        	'deptlist button[action=upd]':{
        		click: this.btnUpdDept        	
        	},
        	'deptediter button[action=save]': { 
                click: this.updateDept 
            }
        });
    },
    delEmp:function(e){
    	var grid = e.ownerCt.ownerCt;
    	//获取选择的行集合.即record数组
		var data = grid.getSelectionModel().getSelection();
		if(data.length == 0){
			Ext.Msg.alert("提示","您最少要选择一条数据");
		}else{
			Ext.Msg.confirm('删除','真的要删除所选择的职员吗?',function(b){
				if(b=='yes'){
					//1.先得到ID的数据(empId)
					var st = grid.getStore();
					var ids = [];
					Ext.Array.each(data,function(record){
						ids.push(record.get('empId'));
					});
					//2.后台操作(delemp)
					Ext.Ajax.request({
						url:'base/delemp.do',
						params:{ids:ids.join(",")},
						method:'POST',
						timeout:3000,
						success:function(response,opts){
							//3.前端操作DOM进行删除(ExtJs)
							Ext.Array.each(data,function(record){
								st.remove(record);
							});
						}
					});
				}
			});
		}
    },
	delDept:function(btn){
		var grid = btn.ownerCt.ownerCt;
		var data = grid.getSelectionModel().getSelection();
		if(data.length == 0){
			Ext.Msg.alert("提示","您最少要选择一条数据");
		}else{
			Ext.Msg.confirm('删除','真的要删除选择的部门吗?',function(b){
				if(b=='yes'){
					var st = grid.getStore();
					
					Ext.Ajax.request({
						url:'base/deldept.do',
						params:{deptId:data[0].get('deptId')},
						method:'POST',
						timeout:3000,
						success:function(response,opts){
							//3.前端操作DOM进行删除(ExtJs)
							Ext.Array.each(data,function(record){
								st.remove(record);
							});
						}
					});
				}
			});
		}
	},
	btnEditEmp:function(btn){
		var grid = btn.ownerCt.ownerCt;
		var data = grid.getSelectionModel().getSelection();
		if(data.length == 0){
			Ext.Msg.alert("提示","您最少要选择一条数据");
		}else{
			var data=grid.getSelectionModel().getSelection();
			var record=data[0];
			this.editEmp(grid,record);
		}
	},
	btnAddEmp:function(btn){
		var grid = btn.ownerCt.ownerCt;
        var record=Ext.create('TS.model.base.EmpModel');
        this.editEmp(grid,record);
	},
	editEmp:function(grid, record){
		var view = Ext.widget('empediter');
        view.down('form').loadRecord(record);
	},
	updateEmp:function(btn){
		var win    = btn.up('window'),
        form   = win.down('form'),
        record = form.getRecord(),
        values = form.getValues();
	    record.set(values);
	    win.close();
	    var store=this.getBaseEmpStoreStore();
	    Ext.Ajax.request({
		    url:'base/updemp.do',
		    method : 'POST',
		    timeout :3000,
		    jsonData:record.data,
		    success:function(response){
			    store.reload();
		    }
	    });
        
	},
	editDept:function(grid, record){
		var view=Ext.widget('deptediter');
		view.down('form').loadRecord(record);
	},
	btnAddDept:function(btn){
		var grid = btn.ownerCt.ownerCt;
        var record=Ext.create('TS.model.base.DeptModel');
        this.editDept(grid,record);
	},
	btnUpdDept:function(btn){
		var grid = btn.ownerCt.ownerCt;
		var data = grid.getSelectionModel().getSelection();
		if(data.length == 0){
			Ext.Msg.alert("提示","您最少要选择一条数据");
		}else{
			var data=grid.getSelectionModel().getSelection();
			var record=data[0];
			this.editDept(grid,record);
		}
	},
	updateDept:function(btn){
		var win    = btn.up('window');
        form   = win.down('form');
        record = form.getRecord();
        values = form.getValues();
	    record.set(values);
	    win.close();
	    var store=this.getBaseDeptStoreStore();
	    Ext.Ajax.request({
				    url:'base/upddept.do',
				    method : 'POST',
				    timeout :3000,
				    jsonData:record.data,
				    success:function(response){				    	
					    store.reload();
                         //console.log("update ok");
				    }
	    });
	    //通过STORER的API进行CU
	    /*if(!record.get('deptId')){
	    	this.getBaseDeptStoreStore().add(record);
	    }
        this.getBaseDeptStoreStore().sync();*/
        
	},
    loadEmpByDeptId:function(grid,record){
    	//actionMethods:{read:'POST'}
    	//extraParams:{deptId:record.get('deptId')}
    	//获取emp store
    	 var store=this.getBaseEmpStoreStore();
    	 //设置store.proxy 请求参数
    	 //store.proxy.extraParams={deptId:record.get('deptId')};
    	 store.load({params: {deptId:record.get('deptId')}});
    }
});